import { toJS } from 'mobx';
import { inject, observer } from 'mobx-react';
import { Button, Form } from 'antd';
import { WeaTop, WeaRightMenu, WeaTab, WeaSearchGroup, WeaFormItem, WeaTools } from 'ecCom';
import { WeaSwitch, WeaTableNew } from 'comsMobx';
import * as Bctt from '../constants/BaseListConstant';

const WeaTable = WeaTableNew.WeaTable;
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;
class BaseList extends React.Component {
  constructor(props) {
    super(props);
  }

  getStore() {

  }

  // render before
  componentWillMount() {
    this.getStore().componentWillMount(this.props.params);
  }

  // render after
  componentDidMount() {
    this.getStore().componentDidMount();
  }

  // component remove before
  componentWillUnmount() {
    this.getStore().componentWillUnmount();
  }

  componentWillReceiveProps(nextProps) {
    const store = this.getStore();
    const oldType = store.routerParams.type;
    const newType = nextProps.params.type;
    // 切换路由或者点击菜单路由刷新组件
    if (oldType !== newType) {
      // component reload
      this.getStore().componentWillUnmount();
      this.getStore().componentWillMount(nextProps.params);
      this.getStore().componentDidMount();
      return;
    }
    const flag = this.props.hasOwnProperty('location');
    if (flag) {
      const keyOld = this.props.location.key;
      const keyNew = nextProps.location.key;
      if (keyOld !== keyNew) {
        // component reload
        this.getStore().componentWillUnmount();
        this.getStore().componentWillMount(nextProps.params);
        this.getStore().componentDidMount();
      }
    }
  }

  render() {
    const store = this.getStore();
    const { loading, titleName, titleIcon, showSearchAd, tableStore, searchKey } = store;

    const weaTableProps = {};// WeaTable中的属性集
    weaTableProps.comsWeaTableStore = tableStore;
    weaTableProps.hasOrder = true;
    weaTableProps.needScroll = true;
    if (this.reRenderColumns) { weaTableProps.getColumns = c => this.reRenderColumns(c); }
    if (this.onOperatesClick) { weaTableProps.onOperatesClick = (record, index, operate, flag) => this.onOperatesClick(record, index, operate, flag); }

    const tableCom = <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@986rt4`} {...weaTableProps} />;

    const searchTypes = this.getTabSearchTypes(store);
    let hasBase = false;
    let hasAdvanced = false;
    for (const t in searchTypes) {
      if (searchTypes[t] == Bctt.TAB_SEARCH_TYPE_BASE) {
        hasBase = true;
      }
      if (searchTypes[t] == Bctt.TAB_SEARCH_TYPE_ADVANCED) {
        hasAdvanced = true;
      }
    }
    let tabCom = null;
    const weaTabProps = {};// WeaTab中的属性集
    weaTabProps.buttons = this.getTabButtons(store);
    weaTabProps.searchType = searchTypes;
    let showWeaTab = false;
    if (hasBase) {
      showWeaTab = true;
      weaTabProps.searchsBaseValue = searchKey;
      weaTabProps.onSearch = store.doSearch;
      weaTabProps.onSearchChange = v => store.setSearchKey(v);
    }

    if (hasAdvanced) {
      showWeaTab = true;
      weaTabProps.showSearchAd = showSearchAd;
      weaTabProps.buttonsAd = this.getAdButtons(store);
      weaTabProps.setShowSearchAd = (bool) => { store.setShowSearchAd(bool); };
      weaTabProps.hideSearchAd = () => store.setShowSearchAd(false);
      weaTabProps.searchsAd = <Form ecId={`${this && this.props && this.props.ecId || ''}_Form@ed0qjj`} horizontal>{this.getSearchConditionForm(store)}</Form>;
    }
    if (showWeaTab) { tabCom = <WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@80csig`} {...weaTabProps} />; }

    return (
      <div className="wea-crmReport-base">
        {tabCom}
        {tableCom}
        {this.renderAppend(store)}
      </div>
    );
  }

  // render custom
  renderAppend(store) {
    return [];
  }

  excelExport(store) {
    store.excelExport(this.excelExportCallBack);
  }

  excelExportCallBack(data) {
    window.location = Bctt.ADDR_EXCEL_EXPORT;
  }

  getRightMenus(store) {
    const { rightMenu, tableStore } = store;
    const { doSearch } = store;
    const btnArr = [];
    const that = this;
    rightMenu && rightMenu.length > 0 && rightMenu.map((m) => {
      const disabled = m.isControl == '1' && toJS(tableStore.selectedRowKeys).length == 0;
      if (!disabled) {
        btnArr.push({
          key: m.type,
          icon: <i className={m.menuIcon} />,
          content: m.menuName,
          onClick: (key) => {
            if (key == Bctt.BTN_EXPORT) {
              that.excelExport(store);
            } else if (key == Bctt.BTN_SEARCH) {
              doSearch();
            } else if (key == Bctt.BTN_COLUMN) {
              tableStore.setColSetVisible(true);
              tableStore.tableColSet(true);
            } else {
              that.rightMenuClick(key);
            }
          },
        });
      }
    });
    return btnArr;
  }

  getTopButtons(store) {
    const { rightMenu, tableStore } = store;
    const btnArr = [];
    const that = this;
    rightMenu && rightMenu.length > 0 && rightMenu.map((m) => {
      m.isTop == '1' && btnArr.length < 4 && btnArr.push(
        <Button
          ecId={`${this && this.props && this.props.ecId || ''}_Button@x6ptj2@${m.type}`}
          type="primary"
          disabled={m.isControl == '1' && toJS(tableStore.selectedRowKeys).length == 0}
          onClick={() => {
            const fn = m.menuFun.indexOf('this') >= 0 ? `${m.menuFun.split('this')[0]})` : m.menuFun;
            if (m.type == Bctt.BTN_EXPORT) {
              that.excelExport(store);
            } else {
              that.rightMenuClick(m.type);
            }
          }}
        >
          {m.menuName}
        </Button>,
      );
    });
    const btnArrCustom = this.getTopButtonsCustom(store);
    return btnArrCustom.concat(btnArr);
  }

  getTopButtonsCustom(store) {
    const btnArr = [];
    return btnArr;
  }

  getAdButtons(store) {
    const { doSearch, setShowSearchAd, clearAdFields } = store;
    return [
      (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@w8y2x1@search`} type="primary" onClick={() => { doSearch(); setShowSearchAd(false); }}>{getLabel(197, '搜索')}</Button>),
      (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@2xkln9@reset`} type="ghost" onClick={() => { clearAdFields(); }}>{getLabel(2022, '重置')}</Button>),
      (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@8lzz5h@cancel`} type="ghost" onClick={() => { setShowSearchAd(false); }}>{getLabel(201, '取消')}</Button>),
    ];
  }

  getTabButtons(store) {
    const arr = [];
    return arr;
  }

  getTabSearchTypes(store) {
    const arr = [];
    return arr;
  }

  getSearchConditionForm(store) {
    const { searchConditionComponents, searchConditionForm } = store;
    const groups = [];
    if (searchConditionComponents) {
      const { isFormInit } = searchConditionForm;
      const formParams = searchConditionForm.getFormParams();
      isFormInit && searchConditionComponents.map((c,indexGroup) => {
        const items = [];
        c.items.map((fields, index) => {
          items.push({
            com: (<WeaFormItem
              ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@zt29w2@${index}`}
              label={`${fields.label}`}
              labelCol={{ span: `${fields.labelcol}` }}
              wrapperCol={{ span: `${fields.fieldcol}` }}
            >
              <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@dkqxw2@${index}`} fieldConfig={fields} form={searchConditionForm} formParams={formParams} onChange={store.searchConditionFieldChange} onEnterSearch={this.onEnterSearch} />
            </WeaFormItem>),
            col: 2,
          });
        });
        groups.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@k04wuz@${indexGroup}`} needTigger title={c.title} showGroup={c.defaultshow} items={items} />);
      });
    }
    return groups;
  }

  rightMenuClick(key) {

  }

  onEnterSearch=() => {
    const store = this.getStore();
    const { doSearch, setShowSearchAd, clearAdFields } = store;
    doSearch();
    setShowSearchAd(false);
  }
}

export default BaseList;
